/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.MyContactDTO;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
//import AbDAO.java;
/**
 *
 * @author Dang Thi Phuong Thao
 */

public class MyContactDAO extends AbDAO {




         private ArrayList<MyContactDTO> capNhatTuResultSet(ResultSet rs) throws SQLException{
        ArrayList<MyContactDTO> Kq = new ArrayList<MyContactDTO>();
        /*String Chuoi = "";
        try {
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                Chuoi += rs.getMetaData().getColumnLabel(i) + ", ";
            }
            JOptionPane.showMessageDialog(null, Chuoi);
        } catch (SQLException ex) {
            Logger.getLogger(MyMailDAO.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        
            //index = rsmd.getColumnCount();
            int index = 0;
            while (rs.next()) {
                index = 1;
                //String temp = rs.getString(index++);
                int Id = rs.getInt(index++);
                String Ten = rs.getString(index++);
                String CongTy = rs.getString(index++);
                String Hinh = rs.getString(index++);
                String Email = rs.getString(index++);
                String DienThoai = rs.getString(index++);
                String NickName = rs.getString(index++);
                String DiaChi = rs.getString(index++);
                
                MyContactDTO myContactDTO = new MyContactDTO(Id, Ten,CongTy,Hinh,Email,DienThoai,NickName,DiaChi);
                Kq.add(myContactDTO);
            }

        return Kq;
         
       // return Kq;
    }
         public ArrayList<MyContactDTO> layTatCaContact () throws SQLException, IOException{
            KetNoi();
            ResultSet rs = thucThiTraVeResultSet("select * from Contact");
            ArrayList<MyContactDTO> Kq = capNhatTuResultSet(rs);
            DongKetNoi();
            return Kq;
         }
         public ArrayList<MyContactDTO> layTatCaContactBangID (String Id) throws SQLException, IOException{
            KetNoi();
            ResultSet rs = thucThiTraVeResultSet("select * from Contact where ID = " + Id) ;
            ArrayList<MyContactDTO> Kq = capNhatTuResultSet(rs);
            DongKetNoi();
            return Kq;
         }
          public int themContact (MyContactDTO contactDTO) throws IOException{
            KetNoi();
        //String date = mailDTO.getM_Ngay().toString().substring(0, "dd-MM-yyyy hh:mm:ss".length());
            String sql = "insert into Contact(Ten,CongTy,Hinh,Email,DienThoai,nickname,DiaChi)" +
                    " values(" + "'" + contactDTO.getM_Ten() + "', " + "'" + contactDTO.getM_CongTy()+ "', " + "'" + contactDTO.getM_Hinh() + "', '" +
                    contactDTO.getM_Email() + "', " + "'" + contactDTO.getM_DienThoai() + "', " + "'" + contactDTO.getM_NickName() + "', '" +
                    contactDTO.getM_DiaChi() +  "'" + ")";
            int Kq = thucThiUpdate(sql);

            DongKetNoi();

            return Kq;
    }
          public int xoaContact(int Id) throws IOException{
            KetNoi();
            String sql = "Delete from Contact Where ID ="+ Id;
            int kq = thucThiUpdate(sql);
            DongKetNoi();
            return kq;
    }

    public int capNhatContact (MyContactDTO mailDTO) throws IOException{
            KetNoi();
            int Kq = 0;
            String sql = "update Contact set Ten = '" + mailDTO.getM_Ten() + "', CongTy = '" + mailDTO.getM_CongTy() + "'," +
                    " Hinh = '" + mailDTO.getM_Hinh() + "', Email = '" + mailDTO.getM_Email() + "', DienThoai = '" + mailDTO.getM_DienThoai() + "', " +
                    "nickname = '" + mailDTO.getM_NickName() + "', " + "DiaChi = '" + mailDTO.getM_DiaChi() + "' where id = " + mailDTO.getM_Id();
            Kq = thucThiUpdate(sql);
            DongKetNoi();

            return Kq;
        }

}
